package com.yunhuajinfu.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ekikousei易皇星 on 16/11/16.
 * E-mail:13764664731@163.com
 * Signature:缘分是本书,翻的不经意会错过,读的太认真会流泪!!
 * <p/>
 * TODO:类描述: 投资页面viewpager指示器
 */
public class XViewPagerIndicator extends LinearLayout {

    private static final String TAG = "ViewPagerIndicator";
    private Context context;
    private ViewPager mViewPager;
    private int COLOR_TEXT_NORMAL = 0xFF000000;
    private int COLOR_INDICATOR_COLOR = Color.RED;

    private String[] mTitles;
    //
    private int mTabCount;
    private float mTranslationX;
    private Paint mPaint = new Paint();
    private int mTabWidth;
    private List<TextView> textViews = new ArrayList<TextView>(); // 装载TextView文本，用来切换字体颜色

    public XViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        mPaint.setColor(COLOR_INDICATOR_COLOR);
        mPaint.setStrokeWidth(9.0f);
    }

    public XViewPagerIndicator(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        mTabWidth = w / mTabCount;
    }
    /**
     *
     * @param indicatorColor  统一设置自己想要的颜色字体
     */
    public void setIndicatorColor(int indicatorColor) {
        this.COLOR_INDICATOR_COLOR = indicatorColor;
        if(null!=mPaint){
            mPaint.setColor(indicatorColor);
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        canvas.translate(mTranslationX, getHeight() - 2);
        canvas.drawLine(0, 0, mTabWidth, 0, mPaint);
        canvas.restore();
    }

    public void scroll(int position, float offset) {

        mTranslationX = getWidth() / mTabCount * (position + offset);
        invalidate();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    /**
     *
     * @param titles
     *            当前ViewPagerIndicator 显示的内容
     * @param viewPager
     *            当前viewPager
     */
    public void setTitles(String[] titles, ViewPager viewPager) {
        mTitles = titles;
        this.mViewPager = viewPager;
        mTabCount = titles.length;
        generateTitleView();

        setViewPagerOnPageChangeListener();

    }

    private void setViewPagerOnPageChangeListener() {
        if (null != mViewPager) {
            mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

                @Override
                public void onPageSelected(int arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    // TODO Auto-generated method stub
                    scroll(position, positionOffset);
                    setCurrentTextColor(position);
                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub

                }
            });
        }

    }

    private void generateTitleView() {
        // TODO Auto-generated method stub
        if (getChildCount() > 0)
            this.removeAllViews();
        final int count = mTitles.length;

        setWeightSum(count);
        for (int i = 0; i < count; i++) {
            TextView tv = new TextView(getContext());
            LayoutParams lp = new LayoutParams(0, LayoutParams.MATCH_PARENT);
            lp.weight = 1;
            tv.setGravity(Gravity.CENTER);
            tv.setTextColor(COLOR_TEXT_NORMAL);
            tv.setText(mTitles[i]);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            tv.setLayoutParams(lp);
            // 事件监听
            final int currentIndex = i;
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Toast.makeText(context, mTitles[currentIndex].toString(),
                    // Toast.LENGTH_SHORT).show();
                    if (null != mViewPager) {

                        mViewPager.setCurrentItem(currentIndex);
                    } else {
                        Log.i(TAG, "请初始化Viewpager");
                    }

                }
            });
            addView(tv);
            textViews.add(tv);

        }

    }

    /**
     *
     *
     * @param currentIndex
     *            设置当前ViewPager滑动时title的颜色
     */
    public void setCurrentTextColor(int currentIndex) {
        for (int i = 0; i < mTitles.length; i++) {
            if (textViews.get(i).equals(textViews.get(currentIndex))) {
                textViews.get(currentIndex).setTextColor(COLOR_INDICATOR_COLOR);
            } else {
                textViews.get(i).setTextColor(COLOR_TEXT_NORMAL);
            }
        }
    }

}
